package io.renren.modules.hos.service.impl;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.entity.DoctorEntity;
import io.renren.modules.hos.entity.PatientEntity;
import io.renren.modules.hos.form.AuthDoctorForm;
import io.renren.modules.hos.form.AuthPatientForm;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.service.PatientService;
import io.renren.modules.hos.vo.HosUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.HosUserDao;
import io.renren.modules.hos.entity.HosUserEntity;
import io.renren.modules.hos.service.HosUserService;


@Service("hosUserService")
public class HosUserServiceImpl extends ServiceImpl<HosUserDao, HosUserEntity> implements HosUserService {
    @Autowired
    private DoctorService doctorservice;
    @Autowired
    private PatientService patientService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HosUserEntity> page = this.page(
                new Query<HosUserEntity>().getPage(params),
                new QueryWrapper<HosUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public HosUserEntity queryUserByPhone(String phone) {
        return baseMapper.queryUserByPhone(phone);
    }


    @Override
    public HosUserVo selectUserById(Long userId) {
        HosUserEntity hosUserEntity = baseMapper.selectById(userId);
        HosUserVo hosUserVo = new HosUserVo();
        hosUserVo.setId(hosUserEntity.getId());
        hosUserVo.setUserImg(hosUserEntity.getUserImg());
        hosUserVo.setNickName(hosUserEntity.getNickName());

        return hosUserVo;
    }

    /**
     * 用户认证医生
     * @return
     */
    @Override
    public boolean authDoctor(AuthDoctorForm authDoctorForm) {
        Long hosUserId = ShiroUtils.getHosUserId();
        HosUserEntity hosUserEntity = this.getById(hosUserId);

        //查询用户是否已认证
        if (hosUserEntity.getAuthIdentity() == 0){
            // 添加医生记录
            DoctorEntity doctorEntity = new DoctorEntity();
            doctorEntity.setUserId(hosUserId);
            doctorEntity.setDoctorName(authDoctorForm.getRealName());
            doctorEntity.setDoctorSex(authDoctorForm.getSex());
            doctorEntity.setDoctorCertid(authDoctorForm.getIdentityCard());
            doctorEntity.setBirthDate(authDoctorForm.getBirthDate());
            doctorEntity.setDoctorImg(authDoctorForm.getDoctorImg());
            doctorEntity.setDoctorDepartment(authDoctorForm.getDep());
            doctorEntity.setDoctorPhone(hosUserEntity.getPhone());
            doctorEntity.setDoctorAddress(authDoctorForm.getAddress());
            doctorEntity.setDoctorTitle(authDoctorForm.getTitle());

            hosUserEntity.setAuthIdentity(2);
            this.updateById(hosUserEntity);
            return doctorservice.save(doctorEntity);
        }else {
            return false;
        }
    }

    @Override
    public boolean authPatient(AuthPatientForm authForm) {
        Long hosUserId = ShiroUtils.getHosUserId();
        HosUserEntity hosUserEntity = this.getById(hosUserId);

        if (hosUserEntity.getAuthIdentity() == 0){
            PatientEntity patientEntity = new PatientEntity();
            patientEntity.setUserId(hosUserId);
            patientEntity.setPatientName(authForm.getPatientName());
            patientEntity.setPatientSex(authForm.getPatientSex());
            patientEntity.setPatientCertid(authForm.getPatientCertid());
            patientEntity.setBirthDate(authForm.getBirthDate());
            patientEntity.setPhone(hosUserEntity.getPhone());
            patientEntity.setMedicalCard(authForm.getMedicalCard());
            patientEntity.setAddress(authForm.getAddress());

            hosUserEntity.setAuthIdentity(1);
            this.updateById(hosUserEntity);
            return patientService.save(patientEntity);
        }else {
            return false;
        }
    }

    @Override
    public PatientEntity selectUserDetailedInfo(Long hosUserId) {
        return patientService.selectByUserId(hosUserId);
    }

    @Override
    public Long selectDoctorId(Long id) {
        DoctorEntity doctorEntity = doctorservice.selectByUserId(id);
        return doctorEntity.getDoctorId();
    }

    @Override
    public Long selectPatientId(Long id) {
        PatientEntity patientEntity = patientService.selectPatient(id);
        return patientEntity.getId();
    }


}